use Cwd;
use strict;

#Checkout the Test_Report from the branch
system("svn checkout http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/Test_Report");

#Get date and time and then print them
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime(time);
my $month = ("January","February","March","April", "May", "June","July", "August",
		"September", "October",  "November", "December")[$mon];
my $timestamp = sprintf "$month\-%02d,%04d-%02d:%02d", $mday, $year + 1900,$hour, $min;
#Write the running time in the Test_Report file
open(FILE,">>Test_Report/$timestamp.txt");
print FILE "RUNNING TESTS\n";
print FILE "RUN TIME: $timestamp\n";
close FILE;
#Run each test suite and write the output in the Test_Report file
print 'Running tests...';
print "\nRunning Test Suite 1";
runTests("TS1.pl","TEST SUITE 1 ");
print "\nRunning Test Suite 2";
runTests("TS2.pl","TEST SUITE 2 ");
print "\nRunning Test Suite 3";
runTests("TS3.pl","TEST SUITE 3 ");
print "\nRunning Test Suite 4";
runTests("TS4.pl","TEST SUITE 4 ");
print "\nRunning Test Suite 5";
runTests("TS5.pl","TEST SUITE 5 ");
print "\nRunning Test Suite 6";
runTests("TS6.pl","TEST SUITE 6 ");
print "\nRunning Test Suite 7";
runTests("TS7.pl","TEST SUITE 7 ");
print "\nFinished \n";

#Enter the Test_Report directory, add the report file, commit to the branch and then copy to the tags folder
my $past_path = getcwd();
chdir("Test_Report");
system("svn add $timestamp.txt");
system("svn commit $timestamp.txt -m \"Adding test report Windows illegal name exception\"");
system("svn copy http://cad-test/repo/crdm/device/firmware/tools/build_tool/branches/Test_Report/$timestamp.txt http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/Test%20-%20reports/ -m \"adding Test Report Windows illegal name exception\"");
#Go back to the previous folder and delete the Test_Report directory
chdir($past_path);
system("rm -rf Test_Report");

#Subroutine to run each test suite and write the output into the test report
sub runTests{
	my $test = $_[0];
	my $comment = $_[1];
	
	open(CMD,"perl $test 2>>Test_Report/$timestamp.txt|");
	my @line = <CMD>;
	open(FILE,">>Test_Report/$timestamp.txt");
	print FILE "-------------------------------------------------------------------------------------------------";
	print FILE "\n\n$comment \n\n";
	print FILE "-------------------------------------------------------------------------------------------------\n";
	print FILE @line;
	close FILE;
	close CMD;
}
